#!/bin/sh
case $? in
	0)
		echo "start script"
		VALUE=`zenity --list \
    --width=600 \
				--height=500 \
		  --title="choose an option:" \
		  --column="Number" --column="Description" \
		    1 "Fastboot tools" \
						2 "ADB tools" \
		    3 "ROM tools" \
		    4 "boot.img tools" \
					 5 "add download link for recovery" \
					 6 "add download link for boot.img" \
					 7 "Download Recovery.img" \
      8 "Download Boot.img" \
					 9 "zipalign" \
		    0 "exit" `
			case $VALUE in
				1)
						CMD=`zenity --list \
    																--width=600 \
																				--height=500 \
		  																--title="choose an option:" \
		  																--column="Number" --column="Description" \
		    1 "flash boot.img" \
		    2 "flash recovery.img" \
		    3 "erase cache partition" \
		    4 "erase system partition" \
		    5 "erase data partition" \
		    6 "print connected devices" \
		    7 "reboot recovery" \
		    8 "reboot" \
		    9 "exit" `	
									case $CMD in
												1)
														zenity --info --text="flash boot.img, in the next screen plese select the boot.img you want to flash"
														FILE=`zenity --file-selection --title= "select boot.img"`
														case $? in
																		0)
																				zenity --question --text="flash $FILE ?"
																				case $? in
																							0)
																									PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for start boot.img flash' --entry`
																									echo $PASSWORD | sudo -S ./tools/fastboot flash boot $FILE
																									zenity --info --text="flash complete! "
																								./zen.sh
																							;;
																							1)
																									zenity --info --text" aborting"
																									./zen.sh
																							esac
																		;;
																		1)
																				zenity --info --text="aborting flash and returning to main menu"
																				./zen.sh
																esac
													;;
													2)	
														zenity --info --text="flash recovery.img, in the next screen plese select the recovery.img you want to flash"
														FILE=`zenity --file-selection --title= "select recovery.img"`
														case $? in
																		0)
																				zenity --question --text="flash $FILE ?"
																				case $? in
																							0)
																									PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for start recovery.img flash' --entry`
																									echo $PASSWORD | sudo -S ./tools/fastboot flash recovery $FILE
																									zenity --info --text="flash complete! "
																								./zen.sh
																						 ;;
																							1)
																									zenity --info --text="aborting"
																									./zen.sh
																						esac
																		;;
																		1)
																				zenity --info --text="aborting flash and returning to main menu"
																				./zen.sh
																esac
													;;
													3)
															zenity --info --text="erasing cache partition"
															zenity --question --text="i'm going to erase cache partition, are you sure?"
															case $? in
															0)
																	PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for start cache partition erasing' --entry`
																											echo $PASSWORD | sudo -S ./tools/fastboot erase cache
																											zenity --info --text="cache partition erasing complete! "
															;;
															1)
																	zenity --info --text="aborting and returning to main menu"
																	./zen.sh
															esac
													;;
													4)
															zenity --info --text="erasing system partition"
															zenity --question --text="i'm going to erase system partition, are you sure?"
															case $? in
															0)
																	PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for start system partition erasing' --entry`
																											echo $PASSWORD | sudo -S ./tools/fastboot erase system
																											zenity --info --text="system partition erasing complete! "
															;;
															1)
																	zenity --info --text="aborting and returning to main menu"
																	./zen.sh
															esac
													;;
													5)
															zenity --info --text="erasing data partition"
															zenity --question --text="i'm going to erase data partition, are you sure?"
															case $? in
															0)
																	PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for start data partition erasing' --entry`
																											echo $PASSWORD | sudo -S ./tools/fastboot erase cache
																											zenity --info --text="data partition erasing complete! "
																											./zen.sh
															;;
															1)
																	zenity --info --text="aborting and returning to main menu"
																	./zen.sh
															esac
													;;
													6)
															zenity --info --text="checking connected devices"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for checking connected devices' --entry`
																											DEVICE=`echo $PASSWORD | sudo -S ./tools/fastboot devices`
																											zenity --info --text="connected devices are:
																											$DEVICE "
																											./zen.sh
													;;
													7)
															zenity --info --text="rebooting in recovery mode"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for reboot in recovery mode' --entry`
																											echo $PASSWORD | sudo -S ./tools/fastboot reboot recovery
																											zenity --info --text="check on device if it has rebooted "
																											./zen.sh
													;;
													8)
															zenity --info --text="rebooting device normally"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for reboot the device' --entry`
																											echo $PASSWORD | sudo -S ./tools/fastboot reboot
																											zenity --info --text="check on device if it has rebooted"
																											./zen.sh
													;;
													9)
															zenity --info --text="returning to main menu"
															./zen.sh
										 esac												
				;;
				2) 
						CMD=`zenity --list \
    																--width=600 \
																				--height=500 \
		  																--title="choose an option:" \
		  																--column="Number" --column="Description" \
		    1 "ADB shell" \
		    2 "reboot" \
		    3 "reboot recovery" \
		    4 "reboot bootloader" \
		    5 "reboot fastboot" \
		    6 "print connected devices" \
		    7 "ADB pull" \
		    8 "ADB push" \
		    9 "exit" `	
										case $CMD in
												1)
														zenity --info --text="starting ADB shell"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for start ADB shell' --entry`
																											echo $PASSWORD | sudo -S ./tools/adb shell
																											./zen.sh
												;;
												2)
														zenity --info --text="rebooting"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for reboot through ADB ' --entry`
																											echo $PASSWORD | sudo -S ./tools/adb reboot
																											./zen.sh
												;;
												3)
														zenity --info --text="rebooting in recovery mode"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for reboot in recovery mode' --entry`
																											echo $PASSWORD | sudo -S ./tools/adb reboot recovery
																											./zen.sh
												;;
												4)
														zenity --info --text="rebooting in bootloader mode"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for reboot in bootloader mode' --entry`
																											echo $PASSWORD | sudo -S ./tools/adb reboot bootloader
																											./zen.sh
												;;
												5)
														zenity --info --text="rebooting in fastboot mode"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for reboot in bootloader mode' --entry`
																											echo $PASSWORD | sudo -S ./tools/adb reboot fastboot
																											./zen.sh
												;;
												6)
														zenity --info --text="printing connected devices"
															PASSWORD=`zenity --title='Password' --hide-text --text='Please enter your root password for print connected devices' --entry`
																											DEVICE=`echo $PASSWORD | sudo -S ./tools/adb devices`
																											zenity --info --text="Devices connected through ADB:
																																																	$DEVICE"
																											./zen.sh
												;;
												7)
														zenity --info --text="starting ADB pull"
														PULL=`zenity --entry --text="entry the partition or file you wish to pull from device"`
														PASSWORD=`zenity --title='Password' --hide-text --text="Please enter your root password for pull $PULL" --entry`
																								echo $PASSWORD | sudo -S ./tools/adb pull $PULL
																								cp -r *.apk pulled
																								echo $PASSWORD | rm -r *.apk
														zenity --info --text="$PULL copied in pulled folder"
														./zen.sh
												;;
												8)
														zenity --info --text="starting ADB push"
														PUSH=`zenity --file-selection --title="select the file you want to push"`
														DIR=`zenity --entry --text="enter the partition on device where you want to push $PUSH "`
														PASSWORD=`zenity --title='Password' --hide-text --text="Please enter your root password for push $PUSH in $DIR " --entry`
																								echo $PASSWORD | sudo -S ./tools/adb remount
																								echo $PASSWORD | sudo -s ./tools/adb push $PUSH $DIR
														./zen.sh
												;;
												9)
														zenity --info --text="returning to main menu"
														./zen.sh
						esac
				;;
								3) 
										CMD=`zenity --list \
    																--width=600 \
																				--height=500 \
		  																--title="choose an option:" \
		  																--column="Number" --column="Description" \
		    1 "Unzip ROM" \
		    2 "zip ROM" \
						0	"exit" `
									case $CMD in
									1)	
									zenity --question --text="proceed with rom unzipping?"
									case $? in
										0)
											FILE=`zenity --file-selection --title= "select rom.zip"`
												case $? in
													0)
														zenity --question --text="do you wish to unzip $FILE ?"
															case $? in
																0)
																	unzip $FILE -d "extracted"
																	zenity --info --text="$FILE successfully extracted to extracted folder! "
															./zen.sh
																;;
																1)
																	zenity --info --text="aborting unzip and returning to main menu"
																	./zen.sh
															esac
													;;
													1)
														zenity --info --text="returning to main menu"
														./zen.sh
												esac
											;;
											1)
												zenity --info --text="returning to main menu"
												./zen.sh
									esac
								;;
								2)
									zenity --question --text="do you wish to zip your rom?"
									case $? in 
										0)
											zenity --info --text="select files you wish to zip:"
											FILES=`zenity --file-selection --separator=" " --multiple --title= "select files"`
														case $? in
																		0)
																			NAME=`zenity --entry \
																							     --title="give a name to your zip" \
																							     --text="insert name" \
																							     --entry-text "YourRom.zip"`
																										case $? in
																														0)
																															zenity --question --text="proceed with the zip of:
																																				 																				$FILES 
																																																									in to the archive $NAME ?"
																															case $? in
																																			0)
																																				cp -r $FILES 'makeupdate/yourfiles'
																																				zip -r "$NAME".zip $FILES
																																				mv $NAME.zip 'makeupdate/outputrom'
																																				./zen.sh
																																				;;
																																				1)
																																					zenity --info --text=" aborting compression and returning to main menu"
																																					./zen.sh
																																			esac
																														;;
																														1)
																															zenity --info --text="returning to main menu"
																															./zen.sh
																														esac
																		;;
																		1)
																			zenity --info --text="returning to main menu"
																			./zen.sh
																		esac	
										;;
										1)
											zenity --info --text="returning to main menu"
											./zen.sh
											esac
								;;
								0)
										zenity --info --text="returning to manin menu"
										./zen.sh
								esac
								esac
								;;
								4)
									zenity --info --text="with this tool you can split your boot.img and merge your ramdisk and kernel.
this tool should don't touch the original boot.img. MAKE A BACKUP OF THE ORIGINAL FILE!! "
									VALUE=`zenity --list \
		  								      --title="choose an option:" \
		 								      --column="Number" --column="Description" \
		   									 1 "split boot.img" \
		   									 2 "make boot.img from kernel+ramdisk" \
		    									 3 "return to main menu"`
											case $? in
															0)
																case $VALUE in
																						1)
																							zenity --info --text="select boot.img"
																							BOOT=`zenity --file-selection --title="select boot.img"`
																							case $? in
																											0)
																												(
																												echo "20" ; sleep 1
																												echo "# splitting boot.img..." ; sleep 1
																												./tools/split_bootimg.pl $BOOT
																												echo "40" ; sleep 1
																												echo "# copying files to splitted_boot dir..."
																												cp *.img-kernel splitted_boot
																												echo "50" ; sleep 1
 								   								   										cp *.img-ramdisk.gz splitted_boot
																												echo "60" ; sleep 1
																												echo "# removing unnecessary files..."
  															      									rm -r *.img-kernel
																												echo "80" ; sleep 1
    								  							  									rm -r *.img-ramdisk.gz
																												echo "100" ; sleep 1
																												echo "# all done" ; sleep 1
																												) |
																												zenity --progress \
																															 --title="unpacking boot.img" \
																															 --text="splitting boot.img" \
																															 --percentage=0
																															./zen.sh
																											;;
																											1)
																												zenity --info --text="aborting and returning to main menu"
																												./zen.sh
																												esac
																			;;
																			2)
																				KERNEL=`zenity --file-selection --title="select boot.img-kernel"`
																								case $? in
																												0)
																													zenity --info --text="select ramdisk.gz"
																													RAMDISK=`zenity --file-selection --title="select ramdisk-new.gz"`
																																	case $? in
																																					0)
																																						(
																																						echo "10" ; sleep 1
																																						echo "# making boot.img" ; sleep 1
																																						echo "40" ; sleep 1
																																						echo "# kernel selected" ; sleep 1
																																						echo "70" ; sleep 1
																																						echo "# ramdisk selected" ; sleep 1
																																						echo "# melting kernel and ramdisk" ; sleep 1
																																						./tools/mkbootimg --cmdline 'no_console_suspend=1 console=null' \
            										          														                  --kernel $KERNEL \
																																												--ramdisk $RAMDISK \
																																												-o repack_boot/boot-new.img
																																						echo "# new boot.img created in repack_boot" ; sleep 1
																																						echo "100" ; sleep 1
																																					  ) |
																																						zenity --progress \
																																									 --title="creating new boot.img" \
																																									 --text="melting kernel and ramdisk" \
																																									 --percentage=0
																																										./zen.sh
																																					;;
																																					1)
																																						zenity --info --text="returning to main menu"
																																						./zen.sh
																																						esac
																																				;;
																																				1)
																																					zenity --info --text="returning to main menu"	
																																					./zen.sh
																																					esac
																											;;
																											3)
																											zenity --into --text="returning to main menu"
																											./zen.sh
																										esac
												;;
												1)
													zenity --info --text="returning to main menu"
													./zen.sh
												esac
        ;;
								5)
											zenity --question --text="add a new download link for recovery.img?"
											case $? in
													0)
															RECOV=`zenity --forms --title="Add Download link" \
														--text="Enter all the required informations" \
														--add-entry="Link URL"`
																case $? in
																0)
																		zenity --info --text="link added"
																		echo " $RECOV " >> recov.txt
																		./zen.sh
																;;
																1)
																		zenity --info --text="return to main menu"
																			./zen.sh
																esac
														;;
														1)
																zenity --info --text="return to main menu"
																./zen.sh
														esac
								;;
								6)
											zenity --question --text="add a new download link for boot.img?"
											case $? in
													0)
															BOOTIMG=`zenity --forms --title="Add Download link for boot.img?" \
														--text="Enter all the required informations" \
														--add-entry="Link URL"`
																case $? in
																0)
																		zenity --info --text="link added"
																		echo " $BOOTIMG " >> boot.txt
																		./zen.sh
																;;
																1)
																		zenity --info --text="return to main menu"
																			./zen.sh
																esac
														;;
														1)
																zenity --info --text="return to main menu"
																./zen.sh
														esac
								;;
								7)
											zenity --question --text="do you wish to download a recovery.img from links list?"
														case $? in
																				0)
																						FILE=`cat recov.txt`
																							DWLR=`zenity --list \
																																			--print-column=ALL \
																																			--column="pick" \
																																			--column="URL" \
																																			--radiolist $FILE`
																						wget $DWLR
																						zenity --info --text="your download is in the zenflasher dir" 
																						./zen.sh
																				;;
																				1)
																						echo "aborting"
																						zenity --info --text="returning to main menu"
																						./zen.sh
																			 esac					
								;;
								8)
												zenity --question --text="do you wish to download a boot.img from links list?"
														case $? in
																				0)
																						FILE=`cat boot.txt`
																							DWLB=`zenity --list \
																																			--print-column=ALL \
																																			--column="pick" \
																																			--column="URL" \
																																			--radiolist $FILE`
																						wget $DWLB
																						zenity --info --text="your download is in the zenflasher folder"
																						./zen.sh
																				;;
																				1)
																						echo "aborting"
																						zenity --info --text="return to main menu"
																						./zen.sh
																			 esac	
								;;
								9)
											ZIP=`zenity --file-selection --title="select the zip you wish to zipalign"`
											ALIGN=`zenity --entry --text="insert alignment in bytes ('4' provides 32-bit alignment"`
										 OUTZ=`zenity --entry --text="give a name at the output zip (remember the .zip extension)"`
											case $? in 
																		 	0)
																		   	zenity --info --text="proceeding with zipalign"
																   			(
												       			./tools/zipalign -f -v $ALIGN $ZIP $OUTZ
													         )|
													       		zenity --progress \
																													--pulsate \
																													--title="zipaligning your file" \
																													--text="zipalign in progress" 
																						(	
																						cp -r $OUTZ zipaligned
																						rm -r $OUTZ
																						) |
																						zenity --progress \
																													--pulsate \
																													--title="copying $OUTZ to zipaligned folder" \
																													--text="cpying in progress" 
																						./zen.sh
																					;;
																					1)
																							zenity --info --text="aborting"
																							./zen.sh
																			esac								
	       ;;
								0)
										echo "quitting script"
										zenity --question --text="do you want to quit zen?"
											case $? in
															0)
																zenity --info --text="quitting zen"
															;;
															1)
																zenity --info --text="returning to main menu"
																./zen.sh
											esac
		 ;;
		 1)	
			echo "stop script"
			;;
		-1)
			echo "error"
esac
